<?php
// 添加mongodb数据库中
$mongo = include 'mongo.php';

$skip = @$_GET['skip']?:0;
// 修改

$title = trim($_GET['title']);
$options = [
	'skip'=>$skip*2,
	'limit'=> 2
];
// 查询有没有条件，字段的显示问题
$where = ['title'=>['$regex' =>'^.*'.$title.'.*']];

// 查询对象
$query = new MongoDB\Driver\Query($where,$options);

// 执行查询  一定要循环
$data = $mongo->executeQuery('php71.article', $query);
$query1 = new MongoDB\Driver\Query($where);

$data1 = $mongo->executeQuery('php71.article', $query1);
$arr = [];
$page = ceil(count($data1->toArray()) /2);

for($i=1;$i<=$page;$i++){
	$arr[$i-1] = [];
}
var_dump($arr);
?>
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>文件列表</title>
	<link rel="stylesheet" type="text/css" href="./static/css/bootstrap.min.css">

		
<link rel="stylesheet" href="//res.layui.com/layui/dist/css/layui.css" media="all">
<link rel="stylesheet" href="//res.layui.com/static/css/global.css" media="all">
<script src="//cdn.bootcss.com/jquery/1.12.3/jquery.min.js"></script>
<script src="//res.layui.com/layui/release/layer/dist/layer.js?v=3111"></script>
</head>
<body>
	<br>
	<div class="container">
	<?php  ?>
		<table class="table table-hover">
			<thead>
				<tr>
					<th>ID</th>
					<th>标题</th>
					<th>内容</th>
					<th>操作</th>
				</tr>
			</thead>
			<tbody>
				<?php foreach($data as $key => $item):  ?>
					<tr>
						<td><?php echo $key+1; ?></td>
						<td><?php echo $item->title ?></td>
						<td><?php echo $item->cnt; ?></td>
						<td>
							<a href="edit.php?title=<?php echo $item->title ?>">修改</a> | 
							<a onclick="delfun('delete.php?title=<?php echo $item->title ?>')">删除</a>
						</td>
					</tr>
				<?php endforeach; ?>
			</tbody>
		</table>
<ul class="pagination">
  <li><a href="#">«</a></li>
  <?php foreach($arr as $key => $item):  ?>
  <li><a href="search.php?title=<?php echo $_GET['title'] ?>&skip=<?php echo $key ?>"><?php echo $key+1 ?></a></li>
  <?php endforeach; ?>
  <li><a href="#">»</a></li>
</ul>
	</div>

	<script src="./static/js/jquery.min.js"></script>
	<script src="./static/js/bootstrap.min.js"></script>

	<script>
		
	 function delfun(url){

	 	// 发送ajax
	 	/*let ret = $.get(url,{},function(ret){
	 		console.log(ret);
	 	},'json')*/

		 layer.confirm('确认删除？', {
  btn: ['删除','撤销'] //按钮
}, function(){
	let ret = $.get(url,{}).then(ret=>{
	 		let obj = JSON.parse(ret);
	 		layer.msg(obj.msg);
	 		// 强制刷新
	 		location.reload(true);
	 	});
});


	 }


	</script>
</body>
</html>